package ar.unlam.com.cliente;

import java.awt.Font;
import java.awt.TextArea;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;

import model.Usuario;
import ar.unlam.com.tateti.blackboard.TaTeTi;

public class PantallaTateti {
	private JFrame frame;
	private JTextField textFieldTuMarcadorTateti;
	private JTextField textFieldElMarcadorTateti;
	private TextArea escribirTatetiTextArea;
	private TextArea historialChatTatetitextArea; 
	private JButton btnEnviarTateti;
	private JButton btnCerrarTateti;
	private TaTeTi juego;

	public PantallaTateti(Usuario usuario){
		initialize(usuario);
	}
	
	public PantallaTateti(int idPartida, Usuario invitador){
		juego = new TaTeTi(this, idPartida, invitador);
		initialize();
	}
	
	private void initialize(Usuario usuario){
		juego = new TaTeTi(this, usuario);
		initialize();
	}
	
	private void initialize(){
		frame = new JFrame();
		frame.setResizable(true);
		frame.setBounds(100, 100, 950, 600);
		
		
		escribirTatetiTextArea = new TextArea();
		historialChatTatetitextArea = new TextArea();
				
		btnEnviarTateti = new JButton("Enviar");
		btnEnviarTateti.setFont(new Font("Dialog", Font.BOLD, 16));		
		btnCerrarTateti = new JButton("Cerrar");
		btnCerrarTateti.setFont(new Font("Dialog", Font.BOLD, 16));
		
		JPanel panelTuMarcadorTateti = new JPanel();
		panelTuMarcadorTateti.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), null));
		JPanel panelElMarcadorTateti = new JPanel();
		panelElMarcadorTateti.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), null));
		JPanel panelIzquierdoTatetiPanel = new JPanel();
		panelIzquierdoTatetiPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		JPanel panelDerechoTateti = new JPanel();
		
		JLabel lblElMarcadorTateti = new JLabel("EL");
		lblElMarcadorTateti.setFont(new Font("Dialog", Font.BOLD, 18));
		JLabel lblTuMarcadorTateti = new JLabel("TU");
		lblTuMarcadorTateti.setFont(new Font("Dialog", Font.BOLD, 18));
		
		textFieldElMarcadorTateti = new JTextField();
		textFieldElMarcadorTateti.setEditable(false);
		textFieldElMarcadorTateti.setColumns(10);		
		textFieldTuMarcadorTateti = new JTextField();
		textFieldTuMarcadorTateti.setEditable(false);
		textFieldTuMarcadorTateti.setColumns(10);		
		
		panelDerechoTateti.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panelIzquierdoTatetiPanel, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panelDerechoTateti, GroupLayout.PREFERRED_SIZE, 557, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(panelIzquierdoTatetiPanel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
						.addComponent(panelDerechoTateti, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
					.addContainerGap(20, Short.MAX_VALUE))
		);
		
		JInternalFrame internalFrame = juego.getTablero(); 
		internalFrame.setVisible(true);
		
		GroupLayout gl_panelDerechoTateti = new GroupLayout(panelDerechoTateti);
		gl_panelDerechoTateti.setHorizontalGroup(
			gl_panelDerechoTateti.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panelDerechoTateti.createSequentialGroup()
					.addGroup(gl_panelDerechoTateti.createParallelGroup(Alignment.TRAILING)
						.addGroup(Alignment.LEADING, gl_panelDerechoTateti.createSequentialGroup()
							.addContainerGap()
							.addComponent(internalFrame, GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE))
						.addGroup(gl_panelDerechoTateti.createSequentialGroup()
							.addGap(12)
							.addComponent(escribirTatetiTextArea, GroupLayout.PREFERRED_SIZE, 405, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panelDerechoTateti.createParallelGroup(Alignment.TRAILING)
								.addComponent(btnCerrarTateti, GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
								.addComponent(btnEnviarTateti, GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))))
					.addContainerGap())
		);
		gl_panelDerechoTateti.setVerticalGroup(
			gl_panelDerechoTateti.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panelDerechoTateti.createSequentialGroup()
					.addContainerGap(20, Short.MAX_VALUE)
					.addComponent(internalFrame, GroupLayout.PREFERRED_SIZE, 421, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_panelDerechoTateti.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_panelDerechoTateti.createSequentialGroup()
							.addComponent(btnEnviarTateti, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(12)
							.addComponent(btnCerrarTateti, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
							.addGap(6))
						.addComponent(escribirTatetiTextArea, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE))
					.addGap(10))
		);
		
		panelDerechoTateti.setLayout(gl_panelDerechoTateti);		
		
		GroupLayout gl_panelElMarcadorTateti = new GroupLayout(panelElMarcadorTateti);
		gl_panelElMarcadorTateti.setHorizontalGroup(
			gl_panelElMarcadorTateti.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelElMarcadorTateti.createSequentialGroup()
					.addGroup(gl_panelElMarcadorTateti.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelElMarcadorTateti.createSequentialGroup()
							.addGap(35)
							.addComponent(lblElMarcadorTateti, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panelElMarcadorTateti.createSequentialGroup()
							.addContainerGap()
							.addComponent(textFieldElMarcadorTateti, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panelElMarcadorTateti.setVerticalGroup(
			gl_panelElMarcadorTateti.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelElMarcadorTateti.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblElMarcadorTateti, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(textFieldElMarcadorTateti, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		panelElMarcadorTateti.setLayout(gl_panelElMarcadorTateti);
		GroupLayout gl_panelIzquierdoTatetiPanel = new GroupLayout(panelIzquierdoTatetiPanel);
		gl_panelIzquierdoTatetiPanel.setHorizontalGroup(
			gl_panelIzquierdoTatetiPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelIzquierdoTatetiPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelIzquierdoTatetiPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(historialChatTatetitextArea, 0, 0, Short.MAX_VALUE)
						.addGroup(gl_panelIzquierdoTatetiPanel.createSequentialGroup()
							.addComponent(panelTuMarcadorTateti, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panelElMarcadorTateti, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(8, Short.MAX_VALUE))
		);
		gl_panelIzquierdoTatetiPanel.setVerticalGroup(
			gl_panelIzquierdoTatetiPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panelIzquierdoTatetiPanel.createSequentialGroup()
					.addGap(25)
					.addComponent(historialChatTatetitextArea, GroupLayout.PREFERRED_SIZE, 409, GroupLayout.PREFERRED_SIZE)
					.addGap(20)
					.addGroup(gl_panelIzquierdoTatetiPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(panelTuMarcadorTateti, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelElMarcadorTateti, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE))
					.addGap(24))
		);	
		
		GroupLayout gl_panelTuMarcadorTateti = new GroupLayout(panelTuMarcadorTateti);
		gl_panelTuMarcadorTateti.setHorizontalGroup(
			gl_panelTuMarcadorTateti.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTuMarcadorTateti.createSequentialGroup()
					.addGroup(gl_panelTuMarcadorTateti.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelTuMarcadorTateti.createSequentialGroup()
							.addGap(33)
							.addComponent(lblTuMarcadorTateti, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panelTuMarcadorTateti.createSequentialGroup()
							.addContainerGap()
							.addComponent(textFieldTuMarcadorTateti, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_panelTuMarcadorTateti.setVerticalGroup(
			gl_panelTuMarcadorTateti.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTuMarcadorTateti.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTuMarcadorTateti, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textFieldTuMarcadorTateti, GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
					.addContainerGap())
		);
		panelTuMarcadorTateti.setLayout(gl_panelTuMarcadorTateti);
		panelIzquierdoTatetiPanel.setLayout(gl_panelIzquierdoTatetiPanel);
		
		frame.getContentPane().setLayout(groupLayout);
		frame.setVisible(true);
	}
	public void setVisible(boolean b) {
		this.frame.setVisible(b);
	}

	public void cerrar() {
		frame.dispose();
	}

}
